package com.datasecops.scannerservices.watermark;


import java.sql.*;

/**
 * @author: GeXiaoLong
 * @time: 2023/2/4 16:16
 */
public class test {

    public static void soul() throws ClassNotFoundException, SQLException {
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgresql://10.26.18.66:5432/text";
        String username = "postgres";
        String password = "sc.9102";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement stmt = conn.createStatement();
        DatabaseMetaData dbmd = conn.getMetaData();
        ResultSet rs = dbmd.getTables(null, null, "user", null);
        if (rs.next()) {
            String tableName = rs.getString("TABLE_NAME");
            String sql = "CREATE TABLE " + tableName + " (\n";
            ResultSet colRet = dbmd.getColumns(null, "%", tableName, "%");
            while (colRet.next()) {
                String columnName = colRet.getString("COLUMN_NAME");
                String dataType = colRet.getString("TYPE_NAME");
                int dataSize = colRet.getInt("COLUMN_SIZE");
                int nullable = colRet.getInt("NULLABLE");
                sql += "\t" + columnName + " " + dataType;
                if (dataSize > 0) {
                    sql += "(" + dataSize + ")";
                }
                if (nullable == 0) {
                    sql += " NOT NULL";
                }
                sql += ",\n";
            }
            sql = sql.substring(0, sql.length() - 2);
            sql += "\n);";
            System.out.println(sql);
        }
        rs.close();
        conn.close();

    }



    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        soul();
    }
}
